/**
 * 
 */
package com.jiang.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Set;
import com.jiang.dao.AbstractDAOImpl;
import com.jiang.dao.IAdminDAO;
import com.jiang.vo.Admin;
import com.jiang.vo.Role;

/**
 * @author Administrator
 *
 */
public class AdminDAOImpl extends AbstractDAOImpl<String> implements IAdminDAO {


	@Override
	public Admin doFind(String aid, String password) throws SQLException {

		Admin admin = null;
		// 前端就应该完成验证，这里不需要再去写验证了
		String sql = "SELECT aid,rid,type,lastdate,flag FROM admin WHERE aid = ? AND password=?  ";
		super.ps = super.conn.prepareStatement(sql);
		super.ps.setString(1, aid);
		super.ps.setString(2, password);
		ResultSet rs = super.ps.executeQuery();
		if (rs.next()) {
			admin = new Admin();
			admin.setAid(rs.getString(1));
			admin.getRole().setRid(rs.getInt(2));
			admin.setType(rs.getInt(3));
			admin.setLastdate(new Date(rs.getTimestamp(4).getTime()));
			admin.setFlag(rs.getInt(5));
		}

		return admin;
	}

	@Override
	public Boolean doUpdateLastDateTime(String aid, Date time) throws SQLException {
		
		String sql = "UPDATE admin SET lastdate=? WHERE aid=? ";
		super.ps = super.conn.prepareStatement(sql);
		super.ps.setTimestamp(1, new Timestamp(time.getTime()));
		super.ps.setString(2, aid);
		return super.ps.executeUpdate()>0;
	}
	
	@Override
	public Boolean doUpdatePassword(String aid, String oldPassword, String newPassword) throws Exception {
		
		String sql = " UPDATE admin SET password=? WHERE aid=? AND password=? ";
		super.ps = super.conn.prepareStatement(sql);
		super.ps.setString(1, newPassword);
		super.ps.setString(2, aid);
		super.ps.setString(3, oldPassword);
		return ps.executeUpdate()>0;
	}
	
	@Override
	public Boolean doCreate(Admin vo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean doRemove(Admin vo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean doRemoveBatch(Set<String> ids) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean doUpdate(Admin vo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Admin doFindById(String id) throws SQLException {
		// 通过aid查询用户
		String sql = "SELECT aid,rid,type,lastdate,flag FROM admin WHERE aid = ? ";
		super.ps = super.conn.prepareStatement(sql);
		super.ps.setString(1, id);
		ResultSet rs = super.ps.executeQuery();
		if(rs.next()){
			Admin admin = new Admin();
			admin.setAid(rs.getString(1));
			Role role = new Role();
			role.setRid(rs.getInt(2));
			admin.setRole(role);
			admin.setType(rs.getInt(3));
			admin.setLastdate(new Date(rs.getTimestamp(4).getTime()));
			admin.setFlag(rs.getInt(5));
			return admin;
		}
		return null;
	}

	@Override
	public List<Admin> doFindAll() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Admin> doFindAllSplit(Integer currentPage, Integer lineSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Admin> doFindAllSplit(Integer currentPage, Integer lineSize, String column, String keyWord)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getAllCount() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getAllCount(String column, String keyWord) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}



}
